<template>
  <div>
    <div class="head-container">
      <el-input v-model="query.orderCode" clearable placeholder="工单编号" style="width: 200px;" class="filter-item" @keyup.enter.native="toQuery" />
      <el-select v-model="query.status" style="width: 200px;" clearable class="filter-item" placeholder="工单状态">
        <el-option
          v-for="(item, index) in statusList"
          :key="item.label + index"
          :label="item.label"
          :value="item.value"
          clearable
          @change="toQuery"
        />
      </el-select>
      <el-input v-model="query.taskAddr" clearable placeholder="详细地址" style="width: 200px;" class="filter-item" @keyup.enter.native="toQuery" />
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
    </div>
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" size="small" style="width: 100%;">
      <el-table-column :show-overflow-tooltip="true" prop="orderCode" label="工单编号" />
      <el-table-column prop="status" label="工单状态">
        <template slot-scope="scope">
          <div v-for="item in statusOptions" :key="item.id">
            <el-tag v-show="scope.row.status.toString() === item.value">{{ item.label }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <!--
      <el-table-column prop="orderSource" label="工单类型">
        <template slot-scope="scope">
          <div v-for="item in orderSourceOptions" :key="item.key">
            <span v-show="scope.row.orderSource.toString() === item.key">{{ item.display_name }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="报修内容">
        <template slot-scope="scope">
          <span v-if="scope.row.orderSource===1">{{ scope.row.taccident.faultDesc }}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="报修时间">
        <template slot-scope="scope">
          <span v-if="scope.row.orderSource===1">{{ parseTime(scope.row.taccident.inputTime) }}</span>
        </template>
      </el-table-column>
      -->
      <el-table-column :show-overflow-tooltip="true" label="任务地区">
        <template slot-scope="scope">
          <span v-if="scope.row.orderSource===2">{{ parseArea(scope.row.task) }}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="详细地址">
        <template slot-scope="scope">
          <span v-if="scope.row.orderSource===2">{{ scope.row.task.taskAddr }}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="任务内容">
        <template slot-scope="scope">
          <span v-if="scope.row.orderSource===2">{{ scope.row.task.taskContent }}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="要求完成时间">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime) }}</span>
        </template>
      </el-table-column>
      <!--
      <el-table-column :show-overflow-tooltip="true" label="发起时间">
        <template slot-scope="scope">
          <span v-if="scope.row.orderSource===2">{{ parseTime(scope.row.task.inputTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="巡检要求">
        <template slot-scope="scope">
          <span v-if="scope.row.orderSource===3">{{ scope.row.inspection.planDesc }}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="巡检时间">
        <template slot-scope="scope">
          <span v-if="scope.row.orderSource===3">{{ parseTime(scope.row.inspection.operateTime) }}</span>
        </template>
      </el-table-column>
      -->
      <el-table-column v-if="checkPermission(['ADMIN','TWORKORDER_ALL','TWORKORDER_EDIT','TWORKORDER_DELETE'])" label="操作" width="120px" align="center">
        <template slot-scope="scope">
          <edit v-permission="['ADMIN','TWORKORDER_ALL','TWORKORDER_EDIT']" :data="scope.row" :sup-this="supThis" />
          <log-order v-permission="['ADMIN','TWORKORDER_ALL','TWORKORDER_EDIT']" :value="scope.row" :sup-this="supThis" />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
  </div>
</template>

<script>
import { get, getDictDetails } from '@/api/dictDetail'
import initData from '@/mixins/initData'
import { parseTime } from '@/utils/index'
import checkPermission from '@/utils/permission'
import edit from '@/views/workorder/order/module/edit'
import LogOrder from '@/views/workorder/order/module/components/logOrder'

export default {
  components: {
    edit,
    LogOrder
  },
  mixins: [initData],
  data() {
    return {
      supThis: this,
      statusOptions: [],
      serviceLevels: [],
      faultTypes: [],
      orderSourceOptions: [
        { key: '1', display_name: '故障单' },
        { key: '2', display_name: '任务单' },
        { key: '3', display_name: '巡检单' }
      ],
      statusList: []
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
      this.getDict('order_status')
      this.getDict('fault_type')
      this.getDict('service_level')
      this.getOrderStates()
    })
  },
  methods: {
    checkPermission,
    parseTime,
    beforeInit() {
      this.url = 'api/tWorkOrder'
      const sort = 'id,desc'
      this.params = { page: this.page, size: this.size, sort: sort }
      const query = this.query
      if (query.orderCode) {
        this.params['orderCode'] = query.orderCode
      }
      if (query.status) {
        this.params['status'] = query.status
      }
      if (query.orderSource) {
        this.params['orderSource'] = query.orderSource
      }
      if (query.customerName) {
        this.params['customerName'] = query.customerName
      }
      if (query.taskAddr) {
        this.params['taskAddr'] = query.taskAddr
      }
      return true
    },
    async getDict(name) {
      return new Promise((resolve, reject) => {
        get(name).then(res => {
          if (name === 'order_status') {
            this.statusOptions = res.content
          } else if (name === 'fault_type') {
            this.faultTypes = res.content
          } else {
            this.serviceLevels = res.content
          }
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    toQuery() {
      this.init()
    },
    search({ customerName }) {
      this.query.customerName = customerName
      this.init()
    },
    getOrderStates() {
      getDictDetails('order_status').then(res => {
        this.statusList = res.content
      })
    },
    parseArea(task) {
      return task.country + '/' + task.province + '/' + task.city + '/' + task.county
    }
  }
}
</script>

<style scoped>
</style>
